//
//  YXClientViewController.m
//  跑跑快销
//
//  Created by 杨潇 on 14-4-28.
//  Copyright (c) 2014年 ylife. All rights reserved.
//
#define nameLabelWidth                  32
#import "YXClientViewController.h"

@interface YXClientViewController ()

@end

@implementation YXClientViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewWillAppear:(BOOL)animated
{
    [self.tabBarController.tabBar setHidden:NO];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    YXClientView * clientView = [[YXClientView alloc] init];
    self.view = clientView;
    _clientView = clientView;
    
    [self addBarButtons];
    self.navigationController.navigationBar.translucent = NO;
    
    //self.tabBarController.tabBar.opaque = YES;
    
    [self initTableView];
    YXWindowInfo * windowInfo = [YXWindowInfo sharedWindowInfo];
//    [NSURLConnection sendRequestWithHeadStr:[[windowInfo userInfoDic] objectForKey:@"ServerAddress"] headValue:nil serviceToken:[[windowInfo userInfoDic] objectForKey:@"serviceToken"] text:[NSDictionary dictionaryWithObjectsAndKeys:@"getStore",@"type",@"2.4",@"ver",@"1",@"pageIndex",@"800",@"pageSize",[[[windowInfo userInfoDic] objectForKey:@"userInfo"] objectForKey:@"AccountID"],@"memberId", nil] dataBlock:^(NSDictionary *dic) {
//        NSLog(@"%@",dic);
//    }];
}

#pragma mark - 初始化asTableView

- (void)initTableView
{
    _clientView.clientTableView.dataSource = self;
    _clientView.clientTableView.delegate = self;

}

#pragma mark - 创建navigationbar上的Item

- (void)addBarButtons
{
    
    // “搜索”按钮
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,50,30)];
    [leftButton setTitle:@"搜索" forState:UIControlStateNormal];
    [leftButton setTitle:@" " forState:UIControlStateHighlighted];
    [leftButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    [leftButton setImage:[UIImage imageNamed:@"a.png"] forState: UIControlStateNormal];
//    [leftButton setImage:[UIImage imageNamed:@"a.png"] forState: UIControlStateHighlighted];
    leftButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [leftButton addTarget:self action:@selector(down:)forControlEvents:UIControlEventTouchUpInside];
    leftButton.tag = 1;
    UIBarButtonItem * searchBtn = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = searchBtn;
    
    // “周边”按钮
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,50,30)];
    [rightButton setTitle:@"周边" forState:UIControlStateNormal];
    [rightButton setTitle:@" " forState:UIControlStateHighlighted];
    [rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    [rightButton setImage:[UIImage imageNamed:@"a.png"] forState: UIControlStateNormal];
//    [rightButton setImage:[UIImage imageNamed:@"a.png"] forState: UIControlStateHighlighted];
    rightButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [rightButton addTarget:self action:@selector(down:)forControlEvents:UIControlEventTouchUpInside];
    rightButton.tag = 2;
    UIBarButtonItem * nearbyBtn = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = nearbyBtn;
}

- (void)down:(UIBarButtonItem *)sender
{
    if (sender.tag == 1) {
        //搜索
        
        [_clientView showSearchView]; 
    } else {
        //周边
        LXNearByViewController *nearByVC = [[LXNearByViewController alloc] init];
        [self.navigationController pushViewController:nearByVC animated:YES];
    }
}

#pragma mark - tableView的代理方法

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 8;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70.0f;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    LXClientTableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[LXClientTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        
    }
    // 标题
    cell.nameLabel.text = @"阳澄华庭";
    CGSize sizeName = [cell.nameLabel.text sizeWithAFont:[UIFont systemFontOfSize:16]];
    [cell.nameLabel setFrame:CGRectMake(45, 5, sizeName.width + nameLabelWidth, 35)];
    
    //五角星
    cell.starImgView.frame = CGRectMake(sizeName.width + nameLabelWidth/4, 10, 16, 16);
    cell.starImgView.hidden = NO;
    
    // 地址
    NSString *address = @"地址：";
    cell.addressLabel.text = [address stringByAppendingString:@"苏州昆山市浦东软件园"];
    CGSize sizeAddress = [cell.addressLabel.text sizeWithAFont:[UIFont systemFontOfSize:13]];
    [cell.addressLabel setFrame:CGRectMake(5, 41, sizeAddress.width, 25)];
    
    // 签到图片
    //cell.signImgView.image =
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    LXClientDetailViewController *detailVC = [[LXClientDetailViewController alloc] init];
    [self.navigationController pushViewController:detailVC animated:YES];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
